package com.cennavi.minemap.dao;

import java.lang.reflect.Field;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;

public abstract class AbstractDao {
	@Autowired
	@Qualifier("pgDataSource")
	public DataSource pgDataSource;
	
	@Autowired
	@Qualifier("authDataSource")
	public DataSource authDataSource;
	
	@Autowired
	@Qualifier("uploadDataSource")
	public DataSource uploadDataSource;

	@Autowired
	public JdbcTemplate jdbcTemplate;
	
	public String getSetSql(Object bean){
		StringBuffer buffer = new StringBuffer();
		try{
			@SuppressWarnings("rawtypes")
			Class cls = bean.getClass();
			Field[] fields = cls.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				Field f = fields[i];
				f.setAccessible(true);
				if(!f.getName().equals("id")){
					if(f.get(bean) != null){
						if("java.lang.String".equals(f.get(bean).getClass().getName())){
							buffer.append(f.getName() + "='" + f.get(bean) + "',");
						}
						if("java.lang.Integer".equals(f.get(bean).getClass().getName())){
							buffer.append(f.getName() + "=" + f.get(bean) + ",");
						}
						if("java.lang.Double".equals(f.get(bean).getClass().getName())){
							buffer.append(f.getName() + "=" + f.get(bean) + ",");
						}
						if("java.lang.Short".equals(f.get(bean).getClass().getName())){
							buffer.append(f.getName() + "=" + f.get(bean) + ",");
						}
					}
				}
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		String sql = buffer.toString();
		
		return sql.equals("")?"":sql.substring(0, sql.length()-1);
	}
}
